package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import com.jgoodies.forms.factories.FormFactory;

import controller.MainController;
import exceptions.MyException;

import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import model.Protokoll;
import model.Termin;

public class ProtokollAnlegenDialog extends JDialog {

	private boolean done;

	private MainController mainController;

	private int patientenId;

	private int terminId;

	private final JPanel panelOben = new JPanel();

	private JComboBox comboBoxTermin;

	private JTextArea textAreaInhalt;

	/**
	 * Create the dialog.
	 */
	public ProtokollAnlegenDialog(JDialog owner, MainController mainController, int patientenId, int terminId) {
		super(owner, true);
		done = false;
		this.mainController = mainController;
		this.patientenId = patientenId;
		this.terminId = terminId;
		
		setTitle("Protokoll anlegen/\u00E4ndern");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 400);
		getContentPane().setLayout(new BorderLayout());
		panelOben.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(panelOben, BorderLayout.CENTER);
		panelOben.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		{
			JLabel labelTermin = new JLabel("Termin");
			panelOben.add(labelTermin, "2, 2, right, default");
		}
		{
			comboBoxTermin = new JComboBox(this.mainController.getTerminController().getTermineOfPatient(this.patientenId).toArray());
			panelOben.add(comboBoxTermin, "4, 2, fill, default");
		}
		{
			JLabel labelInhalt = new JLabel("Inhalt");
			panelOben.add(labelInhalt, "2, 4");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			panelOben.add(scrollPane, "4, 4, fill, fill");
			{
				textAreaInhalt = new JTextArea();
				scrollPane.setViewportView(textAreaInhalt);
			}
		}
		{
			JPanel panelUnten = new JPanel();
			panelUnten.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(panelUnten, BorderLayout.SOUTH);
			{
				JButton buttonOK = new JButton("OK");
				buttonOK.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Termin termin = (Termin) comboBoxTermin.getSelectedItem();
						int terminId = (termin == null) ? -1 : termin.getTerminId();
						
						String inhalt = textAreaInhalt.getText();
						
						try {
							ProtokollAnlegenDialog.this.mainController.getPatientenController().updateProtokoll(ProtokollAnlegenDialog.this.patientenId, terminId, inhalt);
							
							done = true;
							setVisible(false);
							dispose();
						}
						catch (MyException myException) {
							JOptionPane.showMessageDialog(null, myException.getMessage());
						}
					}
				});
				buttonOK.setActionCommand("OK");
				panelUnten.add(buttonOK);
				getRootPane().setDefaultButton(buttonOK);
			}
			{
				JButton buttonAbbrechen = new JButton("Abbrechen");
				buttonAbbrechen.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
						dispose();
					}
				});
				buttonAbbrechen.setActionCommand("Cancel");
				panelUnten.add(buttonAbbrechen);
			}
		}
		
		if (this.terminId >= 0) {
			Protokoll protokoll = this.mainController.getPatientenController().getProtokoll(this.patientenId, this.terminId);
			
			comboBoxTermin.setSelectedItem(protokoll.getTermin());
			
			textAreaInhalt.setText(protokoll.getInhalt());
		}
	}

	public boolean showDialog() {
		setVisible(true);
		return done;
	}

}
